<?php

namespace App\Service\Circle;

use App\Models\Circle\FriendCircleModel;

use DB;

class CircleService
{
    public static function getBestPage($uid = '',$page, $offset = 0)
    {
        $result = DB::select("select praise_num,comment_num,
(select count(*) from lsj_friend_praise_log b where b.circleid=a.id and b.uid=?) as is_praise,
(select count(*) from lsj_follow b where a.uid=b.fuid and b.uid=?) as is_follow,
a.id as topic_id,a.uid as member_id,a.content as topic_text,a.photos as topic_image,
b.nickname as member_name,b.headimgurl as member_avatar,a.created_at as system_create_time,location,a.reading
from lsj_friend_circle a
left join lsj_user b on a.uid=b.uid
order by a.id desc
limit ?,?",[$uid,$uid,$offset,$page]);
        return $result;
    }

    public static function addFriendCircle(array $data)
    {
        $result = FriendCircleModel::insertGetId($data);
        return $result;
    }
	
	/**
     * 删除朋友圈
     */
    public static function removeCircle($circleid)
    {
        if( !FriendCircleModel::where('id',$circleid)->delete() ){
            return false;
        }
        return true;
    }
	
    public static function findFriendCircle(array $data)
    {
        $result = FriendCircleModel::where('id',$data['id'])
            ->first();
        return $result;
    }
	
	/**
     * 获得我的动态
     */
    public static function getMyPushCircle($uid ='',$page,$offset = 0)
    {
        $result = DB::select("select praise_num,comment_num,
(select count(*) from lsj_friend_praise_log b where b.circleid=a.id and b.uid=?) as is_praise,
a.id as topic_id,a.uid as member_id,a.content as topic_text,a.photos as topic_image,
b.nickname as member_name,b.headimgurl as member_avatar,a.created_at as system_create_time,a.reading
from lsj_friend_circle a
left join lsj_user b on a.uid=b.uid
where a.uid=?
order by a.id desc
limit ?,?",[$uid,$uid,$offset,$page]);
		
        return $result;
    }

    /**
     * 获得附近的求购
     */
    public static function getNearCircle($uid, $lon, $lat, $offset)
    {
        $result = DB::select("SELECT  (  
          3959 * acos (  
          cos ( radians(?) )  
          * cos( radians( lat ) )  
          * cos( radians( lon ) - radians(?) )  
          + sin ( radians(?) )  
          * sin( radians( lat ) )  
        )  
      ) AS distance,praise_num,comment_num,
	(select count(*) from lsj_friend_praise_log b where b.circleid=a.id and b.uid=?) as is_praise,
	(select count(*) from lsj_follow b where a.uid=b.fuid and b.uid=?) as is_follow,
	a.id as topic_id,a.uid as member_id,a.content as topic_text,a.photos as topic_image,
	b.nickname as member_name,b.headimgurl as member_avatar,a.created_at as system_create_time,location,a.reading  
    FROM lsj_friend_circle a
    left join lsj_user b on a.uid=b.uid
	where a.lat!=0 and a.uid!=?
    HAVING distance < 20 
    ORDER BY distance  
    LIMIT ?,10",[$lat,$lon,$lat,$uid,$uid,$uid,$offset]);
        return $result;
    }

    /**
     * 增加点赞数量
     */
    public static function addCirclePraiseNum($circle_id)
    {
        FriendCircleModel::where('id',$circle_id)->increment('praise_num');
        return true;
    }

    /**
     * 增加评论数量
     */
    public static function addCircleCommentNum($circle_id)
    {
        FriendCircleModel::where('id',$circle_id)->increment('comment_num');
        return true;
    }

    /**
     * 增加阅读量
     */
    public static function addCircleReadingNum($circle_id)
    {
        FriendCircleModel::where('id',$circle_id)->increment('reading');
        return true;
    }
}